﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;

public partial class Admin_TinTuc_CNTinTuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpTinTuc + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
        }
    }
    protected void BindData(int IdCategories)
    {
        using (TinTuc ent_TinTuc = DataRepository.TinTucProvider.GetById(IdCategories))
        {
            ent_TinTuc.LuotXem = Convert.ToInt32(H_LuotXem.Value);
            txtTieuDe.Text = ent_TinTuc.TieuDe;
            H_AnhDaiDien.Value = ent_TinTuc.AnhDaiDien;
            imgViewer.ImageUrl = ent_TinTuc.AnhDaiDien.Length > 0 ? Common.sPathUpTinTuc + ent_TinTuc.AnhDaiDien : "no_image.jpg";
            txtTomTat.Text = ent_TinTuc.TomTat;
            txtNoiDung.Text = Server.HtmlDecode(ent_TinTuc.NoiDung);
        }

    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            using (TinTuc ent_TinTuc = new TinTuc() { TieuDe = Common.StandardString(txtTieuDe.Text), TomTat = Common.StandardString(txtTomTat.Text), NoiDung = Server.HtmlEncode(txtNoiDung.Text) })
            {
                if (H_AnhDaiDien.Value.Length == 0)
                {
                    ent_TinTuc.AnhDaiDien = "no_image.jpg";
                }
                else
                {
                    ent_TinTuc.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
                }
                ent_TinTuc.LuotXem = Convert.ToInt32(H_LuotXem.Value);
                ent_TinTuc.TrangThai = chkTrangThai.Checked;
                ent_TinTuc.NgayCapNhat = Common.GetServerDateTime();
                if (H_CatalogId.Value == "")
                {
                    updateStatus = DataRepository.TinTucProvider.Insert(ent_TinTuc);
                }
                else
                {
                    ent_TinTuc.Id = Convert.ToInt32(H_CatalogId.Value);
                    ent_TinTuc.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.TinTucProvider.Update(ent_TinTuc);
                    BindData(Convert.ToInt32(H_CatalogId.Value));
                    cmdNhapTiep_Click(sender, e);
                }
                if (!updateStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TINTUC_CAPNHAT_THATBAI, true);
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.TINTUC_CAPNHAT_THANHCONG, true);
                    RedirectTo("~/Admin/TinTuc/QLTinTuc.aspx");
                }
            }

        }

        catch (Exception ex)
        {
            Common.WriteLog("CNTinTuc.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TINTUC_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "";
        H_AnhDaiDien.Value = "no_image.jpg";
        txtTieuDe.Text = "";
        txtTomTat.Text = "";
        chkTrangThai.Checked = true;
        txtNoiDung.Text = "";
    }

    private void RedirectTo(string url)
    {
        string redirectURL = Page.ResolveClientUrl(url);
        string script = String.Format("window.location = '{0}';", redirectURL);
        ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    }
}